<template>
  <div :style="bottomStaySpace()" class="wrap">
    <template v-if="holdMainBodyList.length > 0">
      <div v-for="item in holdMainBodyList" :key="item.id" class="form_list_has_title">
        <div class="form_part">
          <div class="form_title">{{ item.name }}</div>
          <div class="flex_sb_cen">
            <div class="form_name">统一信用代码：</div>
            <div class="form_text">{{ item.creditCode }}</div>
          </div>
          <div class="flex_sb_cen">
            <div class="form_name">审核状态：</div>
            <div :style="{color: statusName(item.type, item.status, item.coopStatus, item.isBind)?.color}" class="form_text">{{ statusName(item.type, item.status, item.coopStatus, item.isBind)?.text }}</div>
          </div>
          <div class="flex_sb_cen">
            <div class="form_name" style="width: 45%">{{ authStatusName(item.type, item.autoSignFinishedStatus, item.status)?.statusTitle }}</div>
            <div :style="{color: authStatusName(item.type, item.autoSignFinishedStatus, item.status)?.color}" class="form_text">{{ authStatusName(item.type, item.autoSignFinishedStatus, item.status)?.text }}</div>
          </div>
        </div>
        <div class="flex_end mt20">
          <template v-if="item.type === 0 && item.status === 2 && item.autoSignFinishedStatus === false">
            <Button :styles="{marginRight: '24rpx'}" @confirm="goAuthSignFn(item.id)">去授权签约</Button>
          </template>
          <template v-if="item.status === 0">
            <Button @confirm="improveInfoFn(item.id, item.type)">完善资料</Button>
          </template>
          <!--  法大大认证状态为（审核中 || 通过）&& 企业审核状态为待完善  -->
          <template v-else-if="(item.status === 1 || item.status === 2) && item.coopStatus === 0">
            <Button @confirm="continueImproveFn(item.id)">继续完善</Button>
          </template>
          <template v-else>
            <Button type="solid" @confirm="watchDetailsFn(item.type, item.status, item.id, item.coopStatus)">查看详情</Button>
          </template>
        </div>
      </div>
    </template>
    <template v-else>
      <div class="flex_cen_cen" style="margin-top: 200rpx">
        <div>
          <Image height="145" name="no_mainBody" oss width="140" />
          <div class="no_main_body">暂无主体</div>
        </div>
      </div>
    </template>
    <div :style="keepBottomSafeDis()" class="suction_bottom_btn">
      <Button size="L" @confirm="addRef.change()">新增合作主体</Button>
    </div>
  </div>
  <Popup ref="addRef">
    <div class="popup_cen">
      <div class="popup_title">
        选择主体类型
        <div class="icon_close" @tap="addRef.change()">
          <Icon name="icon-shanchu-4" size="28" />
        </div>
      </div>
      <div class="flex_cen_cen mt48">
        <Button :styles="{fontSize: '32rpx'}" size="N" type="solid" @confirm="addCooperateMainBodyFn('login_prm',true)">新增工作室</Button>
      </div>
      <div class="flex_cen_cen mt26">
        <Button :styles="{fontSize: '32rpx'}" size="N" @confirm="addCooperateMainBodyFn('new_add_business_license',false)">新增外部其他企业</Button>
      </div>
    </div>
  </Popup>
</template>
<script lang="ts" setup>
import Button from '@C/Button.vue'
import Icon from '@C/Icon.vue'
import { bottomStaySpace, keepBottomSafeDis, onShow, Router, storeCommit } from '@U/common'
import { computed, ref } from 'vue'
import Popup from '@C/Popup.vue'
import Image from '@C/Image.vue'
import { MsgLoading } from '@C/Msg'
import { getHoldMainBodyList, getMeidaoAccountId, getStudioAutoSignLink } from './request'

const addRef = ref()
const statusName = computed(() => (type: number, status: number, coopStatus: number, isBind: boolean) => {
  // status 0 待填写 | status 1 审核中 | status 2 已通过 | status 3 已驳回
  // coopStatus 0 资料待填写 | coopStatus 1 待审核 | coopStatus 2 已通过
  // 工作室不需要通过 coopStatus判断，只有一个status状态
  // isBind 企业是否解绑 true 绑定；false 已解绑
  let obj = {}
  if (status === 0) obj = { text: '待完善', color: '#FF8002' }
  // 工作室
  if (type === 0) {
    if (status === 1) obj = { text: '审核中', color: '#1890FF' }
    if (status === 2) obj = { text: '已通过', color: '#2DC76D' }
    if (status === 3) obj = { text: '已驳回', color: '#FF8002' }
  }
  // 外部企业
  if (type === 1) {
    if (status === 1) obj = { text: '审核中', color: '#1890FF' }
    if (status === 2) {
      if (coopStatus === 0) obj = { text: '待完善', color: '#FF8002' }
      if (coopStatus === 1) obj = { text: '审核中', color: '#1890FF' }
      if (coopStatus === 2) obj = { text: '已通过', color: '#2DC76D' }
      if (coopStatus === 3) obj = { text: '已驳回', color: '#FF8002' }
    }
    if (status === 3) obj = { text: '已驳回', color: '#FF8002' }
    if (isBind === false) obj = { text: '已解绑', color: '#B9BBB9' }
  }
  return obj
})
const authStatusName = computed(() => (type: number, autoSignFinishedStatus: boolean, profileStatus: number) => {
  let obj = {}
  // 工作室
  if (type === 0) {
    if (!autoSignFinishedStatus) obj = { text: '未签约', color: '#FF8002', statusTitle: '自动签约授权状态：' }
    if (autoSignFinishedStatus) obj = { text: '已签约', color: '#2DC76D', statusTitle: '自动签约授权状态：' }
  }
  // 外部企业
  if (type === 1) {
    if (profileStatus === 1) obj = { text: '未授权', color: '#FF8002', statusTitle: '企业授权状态：' }
    if (profileStatus === 2) obj = { text: '已授权', color: '#2DC76D', statusTitle: '企业授权状态：' }
  }
  return obj
})
const goAuthSignFn = async (id: string) => {
  await MsgLoading()
  const res = await getStudioAutoSignLink({ id }).finally(() => {})
  if (res?.code === 200) {
    uni.hideLoading()
    storeCommit('contractPath', res.data)
    Router.push('webviewPreview')
  }
}
const improveInfoFn = (id: string, type: number) => {
  if (type === 0) {
    Router.push('improve_studio_info', { id })
  }
  if (type === 1) {
    Router.push('new_add_business_license', { id })
  }
}
const continueImproveFn = (id: string) => {
  Router.push('new_add_bank_prove_material', { id })
}
const watchDetailsFn = (type: number, status: number, id: string, coopStatus: number) => {
  // type 0 工作室  type 1 外部企业
  if (type === 0 || (type === 1 && status === 2 && coopStatus === 2)) Router.push('hold_main_body_details', { type, id })
  else Router.push('material_review', { id })
}
const addCooperateMainBodyFn = async (urlName: string, issd: boolean) => {
  let param = {}
  // 工作室
  if (issd) {
    const res = await getMeidaoAccountId().finally(() => {})
    if (res?.code === 200) {
      param = { accountid: res.data }
    } else {
      return false
    }
  }
  Router.push(urlName, param)
  addRef.value?.change()
}
const holdMainBodyList = ref([])
// 获取智客持有主体列表
const getHoldMainBodyListFn = async () => {
  await MsgLoading()
  const { data, code } = await getHoldMainBodyList().finally(() => {})
  if (code === 200) {
    uni.hideLoading()
    holdMainBodyList.value = data
  }
}
onShow(() => {
  getHoldMainBodyListFn()
})
</script>

<style lang="less" scoped>
.wrap {
  padding: 24rpx;
  .no_main_body {
    font-weight: 400;
    font-size: 28rpx;
    color: #8C8C8C;
    text-align: center;
    line-height: 40rpx;
  }
}
</style>
